package cn.cuilan.hutool.http.server.action;

import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import cn.hutool.http.server.action.Action;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.cuilan.hutool.http.server.utils.Result;

import java.io.IOException;

/**
 * @author zhangyan
 * @since 2022/6/22
 */
public class FirePushAction implements Action {

    public static final String URI = "/simple/push";

    @Override
    public void doAction(HttpServerRequest request, HttpServerResponse response) throws IOException {
        response.setHeader("Content-Type", "application/json");

        String method = request.getMethod();
        if (method.equalsIgnoreCase("GET")) {
            response.write(JSONUtil.toJsonStr(new Result(405, "不支持的请求方式")));
            return;
        }
        String body = request.getBody();
        JSONObject json = new JSONObject(body);
        System.out.println("\n接收到火点数据 --->");
        String token = json.get("token").toString();
        System.out.println("request param token: " + token);
        System.out.println("request body:");
        System.out.println(json.toStringPretty());

        response.write("{\"token\":" + token + "}");
        //response.write(JSONUtil.toJsonStr(Result.success("fire data received!")));
    }
}
